package com.example.model;

import com.example.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Scanner;

public class Main {
    private static SessionFactory sessionFactory;

    static {
        // 初始化 Hibernate SessionFactory
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("----------欢迎来到用户管理系统!----------");
            System.out.println("请选择你要进行的操作：");
            System.out.println("1.增加用户");
            System.out.println("2.删除用户");
            System.out.println("3.修改用户");
            System.out.println("4.查询用户");
            System.out.println("5.退出");
            String index = scanner.next();
            switch (index) {
                case "1":
                    addUser();
                    break;
                case "2":
                    deleteUser();
                    break;
                case "3":
                    updateUser();
                    break;
                case "4":
                    queryUser();
                    break;
                case "5":
                    System.out.println("退出成功！");
                    System.exit(0);
                default:
                    System.out.println("没有这个选项");
            }
        }
    }

    public static void addUser() {
        Scanner scanner = new Scanner(System.in);
        Session session = sessionFactory.openSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();

            System.out.println("请输入要增加的Id:");
            String id = scanner.next();

            // 检查是否已存在该Id
            User existingUser = session.get(User.class, id);
            if (existingUser != null) {
                System.out.println("该Id已存在，添加失败！");
                return;
            }

            User user = new User();
            user.setId(id);

            System.out.println("请输入要增加的姓名:");
            user.setName(scanner.next());

            System.out.println("请输入要增加的性别:");
            user.setSex(scanner.next());

            System.out.println("请输入要增加的年龄:");
            user.setAge(scanner.nextInt());

            System.out.println("请输入要增加的电子邮件:");
            user.setEmail(scanner.next());

            System.out.println("请输入要增加的电话号码:");
            user.setPhoneNumber(scanner.next());

            session.save(user);
            transaction.commit();
            System.out.println("添加用户成功！");
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            System.out.println("添加用户时发生错误：" + e.getMessage());
        } finally {
            session.close();
        }
    }
    public static void deleteUser() {
        Scanner scanner = new Scanner(System.in);
        Session session = sessionFactory.openSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();

            System.out.println("请输入要删除的Id:");
            String id = scanner.next();

            User user = session.get(User.class, id);
            if (user == null) {
                System.out.println("该Id不存在，删除失败！");
            } else {
                session.delete(user);
                transaction.commit();
                System.out.println("用户删除成功！");
            }
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            System.out.println("删除用户时发生错误：" + e.getMessage());
        } finally {
            session.close();
        }
    }
    public static void updateUser() {
        Scanner scanner = new Scanner(System.in);
        Session session = sessionFactory.openSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();

            System.out.println("请输入要修改的Id:");
            String id = scanner.next();

            User user = session.get(User.class, id);
            if (user == null) {
                System.out.println("该Id不存在，修改失败！");
                return;
            }

            System.out.println("请输入要修改的姓名:");
            user.setName(scanner.next());

            System.out.println("请输入要修改的性别:");
            user.setSex(scanner.next());

            System.out.println("请输入要修改的年龄:");
            user.setAge(scanner.nextInt());

            System.out.println("请输入要修改的电子邮件:");
            user.setEmail(scanner.next());

            System.out.println("请输入要修改的电话号码:");
            user.setPhoneNumber(scanner.next());

            session.update(user);
            transaction.commit();
            System.out.println("用户信息修改成功！");
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            System.out.println("修改用户时发生错误：" + e.getMessage());
        } finally {
            session.close();
        }
    }
    public static void queryUser() {
        Session session = sessionFactory.openSession();

        try {
            System.out.println("Id\t\t姓名\t性别\t年龄\t电子邮件\t\t电话号码");
            for (User user : session.createQuery("FROM User", User.class).list()) {
                System.out.println(user.getId() + "\t\t" + user.getName() + "\t" + user.getSex()
                        + "\t" + user.getAge() + "\t" + user.getEmail() + "\t\t" + user.getPhoneNumber());
            }
        } catch (Exception e) {
            System.out.println("查询用户时发生错误：" + e.getMessage());
        } finally {
            session.close();
        }
    }
}